package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class ExcelToQRCode {
	public static void main(String[] args) throws FileNotFoundException, IOException, WriterException {
			changeMapToQRCode(changeFileToMap());
	}

	public static Map<String, String> changeFileToMap() throws IOException {
		File file = new File("d:\\Infosys Contact.xls");
		HSSFWorkbook workBook = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = workBook.getSheetAt(0);
		Iterator<Row> rows = sheet.rowIterator();
		Map<String, String> map = new HashMap<>();
		while (rows.hasNext()) {
			HSSFRow row = (HSSFRow) rows.next();
			HSSFCell name = row.getCell(1);
			HSSFCell phone = row.getCell(4);
			if(name != null && phone != null){
				String phoneStr = phone.getStringCellValue();
				if(!validatePhone(phoneStr)){
					continue;
				}
				phoneStr = phoneStr.replace("-", "");
				map.put(name.getStringCellValue(), phoneStr);
			}
		}
		workBook.close();
		System.out.println(map);
		return map;
	}
	
	public static boolean validatePhone(String str){
		Pattern pattern = Pattern.compile("^\\d{0,}-\\d{0,}-\\d{0,}$");
		Matcher matcher = pattern.matcher(str);
		System.out.println(str+ " "+matcher.matches());
		return matcher.matches();
	}

	public static void changeMapToQRCode(Map<String, String> map) throws IOException, WriterException {
		String prePath = "d:\\qrcode\\";
		File preFile = new File(prePath);
		if( !preFile.exists()){
			preFile.mkdirs();
		}
		for (Entry<String, String> entry : map.entrySet()) {
			String name = entry.getKey();
			String phone = entry.getValue();
			String str = "BEGIN:VCARD" + "\nVERSION:3.0" 
					+ "\nN:" + name 
					+ "\nTEL:" + phone + "\nEND:VCARD";
			String path = prePath + name+"-"+phone+ ".png";
			BitMatrix byteMatrix;
			String encodeStr = new String(str.getBytes("utf-8"), "iso-8859-1");
			byteMatrix = new MultiFormatWriter().encode(encodeStr, BarcodeFormat.QR_CODE, 240, 240);
			File file = new File(path);

			MatrixToImageWriter.writeToPath(byteMatrix, "png", file.toPath());
			// ImageIO.write(MatrixToImageWriter.toBufferedImage(byteMatrix),
			// "jpg", new FileImageOutputStream(file));
			System.out.println("-----Generate QR Code success.----------");
		}
	}
}
